<template>
  <div class="newAddress">
    <div class="newtop">
      <van-nav-bar title="新增地址"   left-arrow @click-left="onClickLeft" />
    </div>
    <div class="conent">
      <van-address-edit :area-list="areaList" show-postal show-delete show-set-default show-search-result
        :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" 
        @save="onSave" 
        @delete="onDelete"/>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant';
import { areaList } from '@vant/area-data';
import { AddRess, UpdateAddRess, DeleteRess, AddRessList } from '@/api/index.js'
export default {
  data() {
    return {
      areaList,
      searchResult: [],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    //功能接口
    //保存
    onSave(content) {
      // console.log(content)
      if (this.$route.query.name) {
        // console.log("empty")
      var id = this.$route.query.addressid
        UpdateAddRess(id,userid, content.name, content.tel, content.province, content.city, content.county, content.addressDetail, content.isDefault).then((ok) => {
            console.log(ok)
          Toast(ok.data.message);
          //查看是否修改成功
          // AddRessList(userid).then((ok) => {
          //   console.log(ok.data.data)
          // })
        })
      } else {
        // console.log("nonooempty")
        var userid = sessionStorage.getItem('userid')
        AddRess(userid, content.name, content.tel, content.province, content.city, content.county, content.addressDetail, content.isDefault).then((ok) => {
          // console.log(ok.data.message)
          Toast(ok.data.message);
          //查看是否添加成功
          // AddRessList(userid).then((ok) => {
          //   console.log(ok.data.data)
          // })
        })

      }
      
    },
    // 删除
    onDelete() {
      // Toast('delete');
      var s = this.$route.query.addressid;
      console.log(s);
      DeleteRess(s).then((ok) => {
        console.log(ok.data.message)
        Toast((ok.data.message)+"成功")
      })
      
    },
  },
  // beforeCreate() {
  //   var userid = sessionStorage.getItem('userid')
  //   AddRessList(userid).then((ok) => {
  //           console.log(ok.data.data)
  //         })
  // },
}

</script>

<style lang="scss" scoped>
.newAddress{
  width: 100%;
  position: relative;
  .newtop{
    background-color: #f2f2f2;
    .van-nav-bar {
    background-color: #f2f2f2;
    border-bottom: 1px solid #ebebeb;
    }
  }
  .setdaares{
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 0.02rem;
    .btn{
      position: absolute;
      width: 3.432rem;
      height: 0.44rem;
      font-size: 0.14rem;
      border: none;
      border-radius: 0.21rem;
      background-color: #ffffff;
      bottom: 0.01rem;
    }
  }
}
</style>